package com.crr.springmq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class FanoutConfig {

    @Bean
    public Queue queue() {
        Map<String, Object> map = new HashMap<>();
        map.put("x-message-ttl", 5000);
        return new Queue("hello", true, false, false, map);
    }

    /**
     * 向容器注入交换器
     *
     * @return
     */
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange("pubsub-ex");
    }

    /**
     * 向容器注入队列
     *
     * @return
     */
    @Bean
    public Queue queueOne() {
        return new Queue("queue-one");
    }

    /**
     * 向容器注入队列
     *
     * @return
     */
    @Bean
    public Queue queueTwo() {
        return new Queue("queue-two");
    }

    /**
     * 将queueOne绑定到交换机上
     *
     * @param fanoutExchange
     * @param queueOne
     * @return
     */
    @Bean
    Binding bindingOne(FanoutExchange fanoutExchange, Queue queueOne) {

        return BindingBuilder.bind(queueOne).to(fanoutExchange);
    }

    /**
     * 将queueTwo绑定到交换机上
     *
     * @param fanoutExchange
     * @param queueTwo
     * @return
     */
    @Bean
    Binding bindingTwo(FanoutExchange fanoutExchange, Queue queueTwo) {
        return BindingBuilder.bind(queueTwo).to(fanoutExchange);
    }
}